package com.itheima.reggie.exception;

import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice//声明异常处理器类，并被springmvc扫描
@Slf4j
public class ProjectExceptionAdvice {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R SQLIntegrityConstraintViolationExceptionDo(SQLIntegrityConstraintViolationException ex){
        String message = ex.getMessage();//错误信息Duplicate entry 'wcc' for key 'idx_username'，主键冲突
        String s = message.split(" ")[2];
        return R.error("该用户名:"+s+"已重复！");
    }

    @ExceptionHandler(CustomException.class)
    public R CustomExceptionRuntime(CustomException ex){
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }



}
